Using Results
The result of a command is the value generated when the command is executed. You can display the result of a command in the Script Editor. For example, if you run the following script,
tell front document of application "Scriptable Text Editor" move word 1 to end of paragraph 1 end telland then choose Show Result from the Controls menu in the Script Editor, you'll see a value such as
word 32 of front document of application "Scriptable Text Editor"You can use a command that returns a result as a value. For example, the Count command in the following statement returns a value: the number of words in the third paragraph.
count words in paragraph 3You can use this statement anywhere a value is required by enclosing the statement in parentheses. For example, the following statement sets the value ofnumWords
to the value returned by the Count command.
set numWords to (count words in paragraph 3)In addition to displaying the result of a command in the result window, AppleScript puts the result into a predefined variable calledresult
. The value remains there until the next command is executed. If the next command does not return a result, the value ofresult
is undefined. The following two commands show how to use theresult
variable to set the value ofnumWords
to the value returned by the Count command:
count words in paragraph 3 set numWords to resultWhen a direct parameter specifies more than one object, the result is a list that contains a value for each object that was handled. Here is an example of a command whose result is a list:
get paragraphs 1 thru 3 of first documentThe result is a list of strings similar to the following. The first string is the value of the first paragraph, the second string is the value of the second paragraph, and the third string is the value of the third paragraph.
{"This is paragraph one.", "This is paragraph two." ¬ "This is paragraph three."}